Components.utils.import("resource://gre/modules/FileUtils.jsm");
var fileult={
	savetxtfile:function(content,name){
		var dir = FileUtils.getDir("ProfD", ["antiyouku"], true);
		var file = dir.append(name);
		// file is nsIFile, content is a string
		var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
					   createInstance(Components.interfaces.nsIFileOutputStream);

		// use 0x02 | 0x10 to open file for appending.
		foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
		// write, create, truncate
		// In a c file operation, we have no need to set file mode with or operation,
		// directly using "r" or "w" usually.

		// if you are sure there will never ever be any non-ascii text in data you can 
		// also call foStream.write(content, content.length) directly
		var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
						createInstance(Components.interfaces.nsIConverterOutputStream);
		converter.init(foStream, "UTF-8", 0, 0);
		converter.writeString(content);
		converter.close(); // this closes foStream
	},
	savebytefile:function(content,name){
		var dir = FileUtils.getDir("ProfD", ["antiyouku"], true);
		var file = dir.append();
		// content already exists			
		var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].
					 createInstance(Components.interfaces.nsIFileOutputStream);
		stream.init(file, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate
					
		stream.write(content, content.length);
		if (stream instanceof Components.interfaces.nsISafeOutputStream) {
			stream.finish();
		} else {
			stream.close();

		}
	},
	loadfile:function(name,charset){
		var res;
//		var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("ProfD", Components.interfaces.nsIFile);
//		file.initWithPath(path);
		file.append('antiyouku');
		file.append(name);
        var is = ulttool.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(ulttool.Ci.nsIFileInputStream);
        is.init(file, 0x01, 256, null);
        var sis = ulttool.Cc["@mozilla.org/scriptableinputstream;1"].createInstance(ulttool.Ci.nsIScriptableInputStream);
        sis.init(is);

        res = sis.read(sis.available());
        is.close();
        
        if (charset !== null) { // use "null" if you want uncoverted data...
            var unicodeConverter = ulttool.Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(ulttool.Ci.nsIScriptableUnicodeConverter);
            try {
                unicodeConverter.charset = charset || "UTF-8";
            } catch(ex) {
                unicodeConverter.charset = "UTF-8";
            }
            res = unicodeConverter.ConvertToUnicode(res);
        }
        return res;
	},
	loadbytefile:function (name){
		var res;
//		var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("ProfD", Components.interfaces.nsIFile);
//		file.initWithPath(path);
		file.append('antiyouku');
		file.append(name);
        var is = ulttool.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(ulttool.Ci.nsIFileInputStream);
        is.init(file, -1, -1, false);
        var bstream = Components.classes["@mozilla.org/binaryinputstream;1"].
              createInstance(Components.interfaces.nsIBinaryInputStream);
		bstream.setInputStream(is);

        res = bstream.readBytes(bstream.available());
        is.close();
        return res;
	}
}